(function(){

'use strict';

angular.module('controllers.faultLocation.pbossOm')
  .controller('cpuResourceController', cpuResourceController);

function cpuResourceController($scope, resourceServer) {
  var cpuEcharts1 = echarts.init(document.getElementById('cpu1'));
  var cpuEcharts2 = echarts.init(document.getElementById('cpu2'));
  var cpuEcharts3 = echarts.init(document.getElementById('cpu3'));
  resourceServer.cpus(function(res) {
    var arr1 = res.data[1];
    var arr2 = res.data[2];
    var arr3 = res.data[3];
    cpuEcharts1.setOption(Echarts(arr1.hostip));
    cpuEcharts2.setOption(Echarts(arr2.hostip));
    cpuEcharts3.setOption(Echarts(arr3.hostip));
    setInterval(function() {
        cpuEcharts1.addData([
          [0, Math.round(Math.random() * 100), false, false]
        ]);
        cpuEcharts2.addData([
          [0, Math.round(Math.random() * 100), false, false]
        ]);
        cpuEcharts3.addData([
          [0, Math.round(Math.random() * 100), false, false]
        ]);
      }, 2000);

  });



  function spliceData(item) {
    var data = item.data;
    var arr = {};
    arr.time = data.map(function(item) {
      return item.time
    });
    return arr;
  }

  function Echarts(titleName) {
    var option = {};
    option.title = {
      text: titleName
    };
    option.tooltip = {
      trigger: 'axis'
    };
    option.calculable = true;
    option.xAxis = [{
      type: 'category',
      boundaryGap: true,
      data: (function() {
        var res = [];
        var len = 60;
        while (len--) {
          res.push('');
        }
        return res;
      })()
    }];
    option.yAxis = [{
      type: 'value',
      name: '使用比率',
      scale: true,
      boundaryGap: [0.2, 0.2]
    }];
    option.series = [{
      type: 'line',
      data: (function() {
        var res = [];
        var len = 60;
        while (len--) {
          res.push(0);
        }
        return res;
      })(),
        smooth:true,
       itemStyle: {normal: {areaStyle: {type: 'default'}}},
    }];
    return option;
  }
}
cpuResourceController.$inject = ['$scope', 'resourceServer'];

})();